package CustomProviders
{
	import LifeParserCommon.ServiceInterface;
	
	import com.adobe.serialization.json.JSON;
	import com.adobe.serialization.json.JSONDecoder;
	import com.adobe.serialization.json.JSONParseError;
	
	import mx.collections.ArrayCollection;
	
	[Bindable]
	public class GMailProvider implements ServiceInterface
	{
		private var jsonData: ArrayCollection	= null;
		private var reqType: String				= null;
		
		public function GMailProvider(rawData:String, reqType: String = "sentMails")
		{
			super();
			this.reqType						= reqType;
			try {
				this.jsonData						= new ArrayCollection(JSON.decode( rawData ));
			}
			catch (error: Error) {
				throw new JSONParseError(rawData);
			}
		}
		
		public function formatDataForInterface(request: String): ArrayCollection
		{
			var idx: int						= 0;
			var requestLabel: String			= "label" + request;
			
			for (idx = 0; idx < this.jsonData.length; idx++)
			{				
				if(this.jsonData[ idx ][ requestLabel ] == undefined)
				{
					var splitArray: Array							= jsonData[ idx ][ "info" ].split(' ');
					var labelStr: String							= splitArray.join(',');
					
					
					var jsonObj: Object								= this.jsonData[ idx ];
					jsonObj[ requestLabel ]							= labelStr;
					jsonObj[ requestLabel + "Name" ]				= "Actor";
					jsonObj[ requestLabel + "displayKeys" ]			= "date,time," + requestLabel +",message";
					jsonObj[ "message" ]							= jsonObj ["subject"];
					jsonObj[ requestLabel + "frequencyParameter" ]	= requestLabel;
					this.jsonData[ idx ]							= jsonObj;
				}
			}
			
			return this.jsonData;			
		}
		
		public function formatDataForSentiments(request: String): ArrayCollection
		{
			var idx: int						= 0;
			var requestLabel: String			= "label" + request;
			var actor: String					= null;
			
			if (this.reqType == "sentMails")
			{
				actor							= "Recipients";
			}
			else
			{
				actor							= "Sender";
			}
			
			for (idx = 0; idx < this.jsonData.length; idx++)
			{				
				if(this.jsonData[ idx ][ requestLabel ] == undefined)
				{
					this.jsonData[ idx ][ requestLabel ]	= "Sentiment Polarity";
				}
				
				jsonData [idx][requestLabel + "dataTip"]	= "<B>" + actor + ": </B>" 
																+ jsonData [idx]["info"]
																+ "<BR/><B>Subject: </B>"
																+ jsonData [idx]["subject"]
																+ "<BR/><B>Positive Words: </B>"
																+ jsonData [idx]["positiveWords"]
																+ "<BR/><B>Negative Words: </B>"
																+ jsonData [idx]["negativeWords"];
				
				jsonData [idx]["message"]					= jsonData [idx]["subject"];
			}
			
			return this.jsonData;			
		}
		
		public function getHourDataProvider(): ArrayCollection
		{
			return this.jsonData;
		}
		
		public function getDayDataProvider(): ArrayCollection
		{			
			return this.formatDataForInterface("Day");
		}
		
		public function getMonthDataProvider(): ArrayCollection
		{
			return this.jsonData;
		}
		
		public function getActivityDataProvider(): ArrayCollection
		{
			return this.formatDataForInterface("Day");
		}
		
		public function getSentimentDataProvider(): ArrayCollection
		{
			return this.formatDataForSentiments("Senti");
		}
		
		public function getMonthFromDate(date: String): int
		{
			return parseInt(date.split('/')[1]);
		}
		
		public function doFormatTimeToHour(time:String): Number
		{
			var hour:int		= 0;
			var min:int			= 0;
			var resHour: Number	= 0.0;
			var timeArray:Array	= time.split(":");
			
			hour	= parseInt(timeArray[0]);
			min		= parseInt(timeArray[1]);
			resHour	= (hour+min/60);
			
			return resHour;
		}
	}
}